1 /*
2 * Copyright (c) 1999, 2001, Oracle and/or its affiliates. All rights reserved.
3 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
4 *
5 * This code is free software; you can redistribute it and/or modify it
6 * under the terms of the GNU General Public License version 2 only, as
7 * published by the Free Software Foundation. Oracle designates this
8 * particular file as subject to the "Classpath" exception as provided
9 * by Oracle in the LICENSE file that accompanied this code.
10 *
11 * This code is distributed in the hope that it will be useful, but WITHOUT
12 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
13 * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
14 * version 2 for more details (a copy is included in the LICENSE file that
15 * accompanied this code).
16 *
17 * You should have received a copy of the GNU General Public License version
18 * 2 along with this work; if not, write to the Free Software Foundation,
19 * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
20 *
21 * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
22 * or visit www.oracle.com if you need additional information or have any
23 * questions.
24 */
25
26 /* Generated By:JavaCC: Do not edit this line. ParseException.java Version 0.7pre6 */
27 package com.sun.tools.example.debug.expr;
28
29 /**
30 * This exception is thrown when parse errors are encountered.
31 * You can explicitly create objects of this exception type by
32 * calling the method generateParseException in the generated
33 * parser.
34 *
35 * You can modify this class to customize your error reporting
36 * mechanisms so long as you retain the public fields.
37 */
38 public class ParseException extends Exception {
39
40 private static final long serialVersionUID = 7978489144303647901L;
41
42 /**
43 * This constructor is used by the method "generateParseException"
44 * in the generated parser. Calling this constructor generates
45 * a new object of this type with the fields "currentToken",
46 * "expectedTokenSequences", and "tokenImage" set. The boolean
47 * flag "specialConstructor" is also set to true to indicate that
48 * this constructor was used to create this object.
49 * This constructor calls its super class with the empty string
50 * to force the "toString" method of parent class "Throwable" to
51 * print the error message in the form:
52 * ParseException: <result of getMessage>
53 */
54 public ParseException(Token currentTokenVal,
55 int[][] expectedTokenSequencesVal,
56 String[] tokenImageVal
57 )
58 {
59 super("");
60 specialConstructor = true;
61 currentToken = currentTokenVal;
62 expectedTokenSequences = expectedTokenSequencesVal;
63 tokenImage = tokenImageVal;
64 }
65
66 /**
67 * The following constructors are for use by you for whatever
68 * purpose you can think of. Constructing the exception in this
69 * manner makes the exception behave in the normal way - i.e., as
70 * documented in the class "Throwable". The fields "errorToken",
71 * "expectedTokenSequences", and "tokenImage" do not contain
72 * relevant information. The JavaCC generated code does not use
73 * these constructors.
74 */
75
76 public ParseException() {
77 super();
78 specialConstructor = false;
79 }
80
81 public ParseException(String message) {
82 super(message);
83 specialConstructor = false;
84 }
85
86 /**
87 * This variable determines which constructor was used to create
88 * this object and thereby affects the semantics of the
89 * "getMessage" method (see below).
90 */
91 protected boolean specialConstructor;
92
93 /**
94 * This is the last token that has been consumed successfully. If
95 * this object has been created due to a parse error, the token
96 * followng this token will (therefore) be the first error token.
97 */
98 public Token currentToken;
99
100 /**
101 * Each entry in this array is an array of integers. Each array
102 * of integers represents a sequence of tokens (by their ordinal
103 * values) that is expected at this point of the parse.
104 */
105 public int[][] expectedTokenSequences;
106
107 /**
108 * This is a reference to the "tokenImage" array of the generated
109 * parser within which the parse error occurred. This array is
110 * defined in the generated ...Constants interface.
111 */
112 public String[] tokenImage;
113
114 /**
115 * This method has the standard behavior when this object has been
116 * created using the standard constructors. Otherwise, it uses
117 * "currentToken" and "expectedTokenSequences" to generate a parse
118 * error message and returns it. If this object has been created
119 * due to a parse error, and you do not catch it (it gets thrown
120 * from the parser), then this method is called during the printing
121 * of the final stack trace, and hence the correct error message
122 * gets displayed.
123 */
124 @Override
125 public String getMessage() {
126 if (!specialConstructor) {
127 return super.getMessage();
128 }
129 String expected = "";
130 int maxSize = 0;
131 for (int[] expectedTokenSequence : expectedTokenSequences) {
132 if (maxSize < expectedTokenSequence.length) {
133 maxSize = expectedTokenSequence.length;
134 }
135 for (int j = 0; j < expectedTokenSequence.length; j++) {
136 expected += tokenImage[expectedTokenSequence[j]] + " ";
137 }
138 if (expectedTokenSequence[expectedTokenSequence.length - 1] != 0) {
139 expected += "...";
140 }
141 expected += eol + " ";
142 }
143 String retval = "Encountered \"";
144 Token tok = currentToken.next;
145 for (int i = 0; i < maxSize; i++) {
146 if (i != 0) {
147 retval += " ";
148 }
149 if (tok.kind == 0) {
150 retval += tokenImage[0];
151 break;
152 }
153 retval += add_escapes(tok.image);
154 tok = tok.next;
155 }
156 retval += "\" at line " + currentToken.next.beginLine + ", column " + currentToken.next.beginColumn + "." + eol;
157 if (expectedTokenSequences.length == 1) {
158 retval += "Was expecting:" + eol + " ";
159 } else {
160 retval += "Was expecting one of:" + eol + " ";
161 }
162 retval += expected;
163 return retval;
164 }
165
166 /**
167 * The end of line string for this machine.
168 */
169 protected String eol = System.getProperty("line.separator", "\n");
170
171 /**
172 * Used to convert raw characters to their escaped version
173 * when these raw version cannot be used as part of an ASCII
174 * string literal.
175 */
176 protected String add_escapes(String str) {
177 StringBuffer retval = new StringBuffer();
178 char ch;
179 for (int i = 0; i < str.length(); i++) {
180 switch (str.charAt(i))
181 {
182 case 0 :
183 continue;
184 case '\b':
185 retval.append("\\b");
186 continue;
187 case '\t':
188 retval.append("\\t");
189 continue;
190 case '\n':
191 retval.append("\\n");
192 continue;
193 case '\f':
194 retval.append("\\f");
195 continue;
196 case '\r':
197 retval.append("\\r");
198 continue;
199 case '\"':
200 retval.append("\\\"");
201 continue;
202 case '\'':
203 retval.append("\\\'");
204 continue;
205 case '\\':
206 retval.append("\\\\");
207 continue;
208 default:
209 if ((ch = str.charAt(i)) < 0x20 || ch > 0x7e) {
210 String s = "0000" + Integer.toString(ch, 16);
211 retval.append("\\u" + s.substring(s.length() - 4, s.length()));
212 } else {
213 retval.append(ch);
214 }
215 continue;
216 }
217 }
218 return retval.toString();
219 }
220
221 }